﻿using Microsoft.AspNetCore.Mvc;
using NLog;
using NLog.Web;
using System;
using System.Collections.Generic;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace SZ.Nlog.MongoDb.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        public Logger _log { get; }

        //加载日志配置信息文件后去捕获所有的错误
        //Logger logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

        public ValuesController()
        {
            _log = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
        }

        // GET: api/<ValuesController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            int a = 10, b = 0;
            try
            {
                //logger.Info("Init Log API Information");
                var result = a / b;
                _log.Info($"计算结果：{result}");
            }
            catch (Exception ex)
            {
                _log.Error($"异常消息：{ex.Message}");
            }
            finally
            {
                LogManager.Shutdown();
            }

            return new[] { "value1", "value2" };
        }

        // GET api/<ValuesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<ValuesController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<ValuesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ValuesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}